//
//  LGInPlayView.swift
//  LGMusic
//
//  Created by 梁尚嘉 on 2017/4/7.
//  Copyright © 2017年 LimeGah. All rights reserved.
//

import UIKit
import SDWebImage
class LGInPlayView: UIView {

    var bgImageView:UIImageView?
    var playBar:LGInPlayBar?
    var progressView:LGInProgressView?
    var songInfoView:LGInPlaySongInfoView?
    var lrcView:LGInPlayLrcShowList?
    var scrollView:UIScrollView?
    var songMaskView:UIView?
    var songListView:LGInPlaySongListView?
    var navigationBar:LGNavigationBar?

    var song:Song? {
        didSet {
            self.loadSong(song!)
        }
    }

    public func loadSong(_ song:Song) {

        self.bgImageView?.sd_setImage(with: URL.init(string: (song.songPicBig!)), placeholderImage: nil, options: SDWebImageOptions.continueInBackground)

        self.songInfoView?.loadSong(song)

        self.lrcView?.lrcItems = []

        LrcHelper.lrcItems(song.lrcLink!) { (lrcItems) in
            self.lrcView?.lrcItems = lrcItems
        }

        self.songListView?.loadSong(song)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configure()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure() -> Void {

        //背景图
        let imageView = UIImageView.init()
        imageView.contentMode = UIViewContentMode.scaleAspectFill
        imageView.clipsToBounds = true
        self.bgImageView = imageView
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }

        //高斯模糊图层
        let blurview = UIVisualEffectView.init(effect: UIBlurEffect.init(style: UIBlurEffectStyle.light))
        self.addSubview(blurview)
        blurview.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }

        //遮罩初始化
        let maskView = UIView.init()
        self.songMaskView = maskView
        maskView.backgroundColor = UIColor.color(hexString: "F6F6F6", alpha: 0.6)
        self.addSubview(maskView)
        maskView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalTo(0)
            make.height.equalTo(SM_SCREEN_SIZE().height*0.55)
        }

        let play_bar_height = SM_SIZE(220)
        //歌曲控制板
        let playBar = LGInPlayBar.init(frame: CGRect.zero)
        self.playBar = playBar
        self.addSubview(playBar)
        playBar.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(play_bar_height)
        }

        let progress_height = SM_SIZE(40)
        //进度控制板
        let progressView = LGInProgressView.init(frame: CGRect.zero)
        self.progressView = progressView
        self.addSubview(progressView)
        progressView.snp.makeConstraints { (make) in
            make.bottom.equalTo(playBar.snp.top)
            make.left.right.equalTo(0)
            make.height.equalTo(progress_height)
        }

        let tool_bar_height = SM_SIZE(150)
        //工具板
        let toolBar = LGInPlayToolBar.toolBar(["bt_playpage_collection",
                                               "bt_playpage_down",
                                               "bt_playpage_share",
                                               "icon_comments_zero",
                                               "bt_playpage_more"])
        self.addSubview(toolBar)
        toolBar.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(progressView.snp.top)
            make.height.equalTo(tool_bar_height)
        }

        let navigation_bar_height = SM_SIZE(128)
        //LGNavigationBar
        let navigationBar = LGNavigationBar.init(frame: CGRect.zero)
        self.addSubview(navigationBar)
        self.navigationBar = navigationBar
        navigationBar.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(navigation_bar_height)
        }

        //歌风信息视图
        let songInfoView = LGInPlaySongInfoView.init(frame: CGRect.zero)
        self.songInfoView = songInfoView
        self.addSubview(songInfoView)
        songInfoView.snp.makeConstraints { (make) in
            make.left.right.equalTo(0)
            make.bottom.equalTo(toolBar.snp.top).offset(SM_SIZE(-45))
        }

        //scrollview initialize
        self.scrollView = UIScrollView.init(frame: CGRect.zero)
        self.scrollView?.contentSize = CGSize(width:SM_SCREEN_SIZE().width*3,height:0)
        self.scrollView?.delegate = self
        self.addSubview(self.scrollView!)
        self.scrollView?.isPagingEnabled = true
        self.scrollView?.showsHorizontalScrollIndicator = false
        self.scrollView?.snp.makeConstraints({ (make) in
            make.left.right.equalTo(0)
            make.top.equalTo(navigationBar.snp.bottom)
            make.bottom.equalTo(toolBar.snp.top)
        })


        let floatViewHeight = SM_SCREEN_SIZE().height
            - play_bar_height - navigation_bar_height - tool_bar_height - progress_height
        let floatViewContentInset = (floatViewHeight-SM_SIZE(100))/2
        //歌词列表页面
        self.lrcView = LGInPlayLrcShowList.init(frame: CGRect.zero)
        self.lrcView?.tableView?.contentInset = UIEdgeInsets.init(top: floatViewContentInset, left: 0, bottom: floatViewContentInset, right: 0)
        self.scrollView?.addSubview(self.lrcView!)
        self.lrcView?.snp.makeConstraints({ (make) in
            make.left.equalTo(SM_SCREEN_SIZE().width*2)
            make.width.equalTo(SM_SCREEN_SIZE().width)
            make.height.equalTo(floatViewHeight)
            make.top.equalTo(0)
        })

        //歌曲列表页面
        self.songListView = LGInPlaySongListView.init(frame: CGRect.zero)
        self.songListView?.delegate = self
        self.scrollView?.addSubview(self.songListView!)
        self.songListView?.snp.makeConstraints({ (make) in
            make.left.top.equalTo(0)
            make.width.equalTo(SM_SCREEN_SIZE().width)
            make.height.equalTo(floatViewHeight)
        })

        self.scrollView?.contentOffset = CGPoint.init(x: SM_SCREEN_SIZE().width, y: 0)
        LGOutPlayBar.sharedBar.delegate = self

    }


    deinit {
        debugPrint("\(self.classForCoder) dealloc")
    }
}

//MARK: UIScrollViewDelegate
extension LGInPlayView:UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let index = round(scrollView.contentOffset.x/SM_SCREEN_SIZE().width)
        var alpha = 1
        var heightPercent = 0.55 as CGFloat
        if index != 1 {
            alpha = 0
            heightPercent = 1
        }

        UIView.animate(withDuration: 0.5,
                       delay: 0,
                       options: UIViewAnimationOptions.allowUserInteraction,
                       animations: {
            self.navigationBar?.currentIndex = UInt(index)
            self.songInfoView?.alpha = CGFloat(alpha)
            self.songMaskView?.snp.updateConstraints({ (make) in
                make.height.equalTo(SM_SCREEN_SIZE().height*heightPercent)
            })
        }, completion: nil)

    }
}

//MARK: LGOutPlayBarDelegate
extension LGInPlayView:LGOutPlayBarDelegate {
    func didSongStartPlay(_ song: Song) {
        self.playBar?.playOrPauseButton?.isSelected = true
        self.song = song
    }

    func didSongPause(_ song: Song) {
        self.playBar?.playOrPauseButton?.isSelected = false
    }

    func didSongContinue(_ song: Song) {
        self.playBar?.playOrPauseButton?.isSelected = true
    }

    func didSongProcess(_ song: Song, _ progress: Double, _ currentDuration: Double, _ totoalDuration: Double) {
        if !(progress.isNaN || currentDuration.isNaN || totoalDuration.isNaN) {
            self.progressView?.totoalDuration = totoalDuration
            self.progressView?.currentDuration = currentDuration
            self.lrcView?.currentDuration = currentDuration
        }
    }
}


//MARK:LGInPlaySongListViewDelegate
extension LGInPlayView: LGInPlaySongListViewDelegate {
    func didSelectSongAtIndex(_ index: Int) {
        LGOutPlayBar.sharedBar.playSong(at:index)
    }
}

